package com.admin.education.controllers.business;

import com.admin.education.common.exceptions.BadRequestException;
import com.admin.education.common.jwt.JwtAuthorize;
import com.admin.education.common.response.IResponseMessage;
import com.admin.education.common.response.ResponseMessage;
import com.admin.education.common.response.ResponseMessageWithData;
import com.admin.education.common.response.Result;
import com.admin.education.dataTransferObject.PageRequest;
import com.admin.education.dataTransferObject.PageResponse;
import com.admin.education.dataTransferObject.ServicesExcuteResult;
import com.admin.education.dataTransferObject.icons.IconsTransfer;
import com.admin.education.dataTransferObject.icons.request.CreateIconsRequest;
import com.admin.education.dataTransferObject.icons.request.UpdateIconsRequest;
import com.admin.education.services.Interfaces.IIconsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.concurrent.Callable;


@JwtAuthorize
@RestController(value = "businessIconsController")
@RequestMapping("icons")
@CrossOrigin(origins = "*", maxAge = 3600)
public class IconsController {
    @Autowired
    private IIconsService iconsService;
    @GetMapping("")
    public Callable<IResponseMessage> queryIcons(@Valid PageRequest pageRequest){
        return new Callable<IResponseMessage>() {
            @Override
            public IResponseMessage call() throws Exception {
                IResponseMessage responseMessage = null;
                ServicesExcuteResult<PageResponse<IconsTransfer>> servicesExcuteResult = iconsService.queryIcons(pageRequest);
                if(servicesExcuteResult.isSuccess()){
                    responseMessage = new ResponseMessageWithData<PageResponse<IconsTransfer>>(Result.Success,servicesExcuteResult.getData());
                }
                else{
                    throw new BadRequestException(servicesExcuteResult.getErrorMessage());
                }
                return  responseMessage;
            }
        };
    }
    @PostMapping("")
    public Callable<IResponseMessage> createIcons(@RequestBody @Valid CreateIconsRequest createIconsRequest){
        return new Callable<IResponseMessage>() {
            @Override
            public IResponseMessage call() throws Exception {
                IResponseMessage responseMessage = null;
                ServicesExcuteResult servicesExcuteResult = iconsService.createIcons(createIconsRequest);
                if(servicesExcuteResult.isSuccess()){
                    responseMessage = new ResponseMessage(Result.Success);
                }
                else{
                    throw  new BadRequestException(servicesExcuteResult.getErrorMessage());
                }
                return responseMessage;
            }
        };
    }
    @PutMapping("{iconsId}")
    public Callable<IResponseMessage> updateIcons(@PathVariable Long iconsId,@RequestBody @Valid UpdateIconsRequest updateIconsRequest){
        return  new Callable<IResponseMessage>() {
            @Override
            public IResponseMessage call() throws Exception {
                IResponseMessage responseMessage = null;
                ServicesExcuteResult servicesExcuteResult = iconsService.updateIcons(iconsId,updateIconsRequest);
                if(servicesExcuteResult.isSuccess()){
                    responseMessage = new ResponseMessage(Result.Success);
                }
                else{
                    throw  new BadRequestException(servicesExcuteResult.getErrorMessage());
                }
                return responseMessage;
            }
        };
    }
    @GetMapping("{iconsId}")
    public Callable<IResponseMessage> queryIconsById(@PathVariable Long iconsId)
    {
        return new Callable<IResponseMessage>() {
            @Override
            public IResponseMessage call() throws Exception {
                IResponseMessage responseMessage = null;
                ServicesExcuteResult<IconsTransfer> servicesExcuteResult = iconsService.queryIconsById(iconsId);
                if(servicesExcuteResult.isSuccess()){
                    responseMessage = new ResponseMessageWithData<IconsTransfer>(Result.Success,servicesExcuteResult.getData());
                }
                else{
                    throw  new BadRequestException(servicesExcuteResult.getErrorMessage());
                }
                return responseMessage;
            }
        };
    }
    @DeleteMapping("{iconsId}")
    public Callable<IResponseMessage> deleteIcons(@PathVariable Long iconsId)
    {
        return new Callable<IResponseMessage>() {
            @Override
            public IResponseMessage call() throws Exception {
                IResponseMessage responseMessage = null;
                ServicesExcuteResult servicesExcuteResult = iconsService.deleteIcons(iconsId);
                if(servicesExcuteResult.isSuccess()){
                    responseMessage = new ResponseMessage(Result.Success);
                }
                else{
                    throw  new BadRequestException(servicesExcuteResult.getErrorMessage());
                }
                return responseMessage;
            }
        };
    }
}
